import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'OperatePlatformM',
  state: {
    //常用功能
    controlCurrent: {},
    xqtxNum: 0, // 行权提醒
    dictMap: {}, //字典查询
    list: [],
    pagination: {},
    // 负面舆情，公司公告
    negativeNewsList: [],
    companyNoticeList: [],
    processList: [], //已办条数
    orgId: null, //权限保存
    storeSuper: [], //存储的第一个的超户的值
  },
  effects: {
    //存储超户的值
    *storeSuper({ payload, callback }, { call, put }) {
      const response = yield call(api.userManageNew.SearchTenant, payload);
      if (response && response.code == 0) {
        let tempArr = [];
        response.data.dataSource &&
          response.data.dataSource.forEach((item) => {
            tempArr.push({ value: item.code, title: item.name, id: item.id });
          });
        callback(tempArr);
      } else {
        message.error(response && response.msg ? response.msg : '查询失败!');
      }
    },
    //权限获取
    *getPermission({ payload, callback }, { call, put }) {
      // const data = yield call(api.OperatePlatform.queryPermission, payload);
      yield put({
        type: 'save',
        payload: {
          proxy: {
            hsdjtx: 1,
            xqtx: 1,
            kzzsh: 1,
            zqyc: 1,
          },
        },
      });
    },
    //通知公告
    *getNoticeAnnounce({ payload, callback }, { call, put }) {
      const response = yield call(api.OperatePlatform.queryNoticeAnnounce, payload);
      if (response.code == 0) {
        callback(
          response.data || {
            normal: [],
            urgent: [],
            noUrgent: [],
          },
        );
      } else {
        message.error('通知公告查询失败！');
        return false;
      }
    },
    //当日债回售提醒
    *queryBondsSoldBackList({ callback, payload }, { call, put }) {
      const response = yield call(api.OperatePlatform().resaleList, payload);
      if (response.code == 0) {
        callback({
          list: response?.data?.dataSource || [],
          pagination: {
            current: response?.data?.pageInfo?.currentPage || 1,
            pageSize: response?.data?.pageInfo?.numInPage || 10,
            total: response?.data?.pageInfo?.total || 0,
          },
        });
      } else {
        message.error('获取当日债回售提醒失败!');
        return;
      }
    },
    //产品到期
    *queryProductTimeArrive({ payload, callback }, { call, put }) {
      const response = yield call(api.OperatePlatform().queryProInfo, payload);
      if (response.code == 0) {
        callback({ data: response?.data || [] });
      } else {
        message.error('产品到期查询失败！');
        return false;
      }
    },
    //获取字典
    *fetchDictByKeys({ payload }, { call, put }) {
      const response = yield call(queryDictByKeys, payload);
      if (response) {
        yield put({
          type: 'save',
          payload: {
            dictMap: response,
          },
        });
        return response;
      } else {
        message.error('查询数据字典失败');
      }
    },
    /**  根据当前登录人  查询常用功能  */
    *queryCuFunctionsByCreator({ payload }, { call, put }) {
      const response = yield call(api.OperatePlatform().queryCuFunctionsByCreator, payload);
      if (response?.code == 0) {
        if (response?.data) {
          yield put({
            type: 'save',
            payload: {
              controlCurrent: {
                myApp: response?.data?.myApp || [],
                otherApp: response?.data?.otherApp || [],
              },
            },
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              controlCurrent: {
                myApp: [],
                otherApp: [],
              },
            },
          });
        }
      } else {
        message.error('查询常用功能失败!');
        return;
      }
    },

    /**  常用功能修改或新增  */
    *updateOrAddCuFunctions({ payload }, { call, put }) {
      const response = yield call(api.OperatePlatform().updateOrAddCuFunctions, payload);
      return response;
    },

    // 信评提醒
    *queryTable({ payload, callback }, { call, put }) {
      // const res = yield call(api.reviewRemind.queryTable, payload);
      const res = {
        success: true,
        data: {
          pageInfo: {
            total: 1,
          },
        },
      };
      if (res && res.success) {
        callback(res.data.pageInfo.total);
      } else {
        callback(0);
        message.error(res.message || '信评提醒暂无数据');
      }
    },
    // 今日会议
    *getMessage({ payload, callback }, { call, put }) {
      // const res = yield call(api.OperatePlatform.getTodayMeet, payload);
      // if (res && res.code == 0) {
      //   callback(res.data)
      // } else {
      //   callback([])
      //   message.error(res && res.message ? res.message : '暂无数据')
      // }
      callback([]);
    },

    // 负面舆情
    *queryNegativeNews({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comNewsList, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            negativeNewsList: response?.data?.dataSource || [],
          },
        });
      } else {
        message.error(response && response.msg ? response.msg : '暂无负面舆情');
      }
    },
    // 公司公告
    *queryCompanyNotice({ payload }, { call, put }) {
      const response = yield call(api.monitorpublicopinion.comAnnouncement, payload);
      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            companyNoticeList: response?.data?.dataSource || [],
          },
        });
      } else {
        message.error(response && response.msg ? response.msg : '暂无公司公告');
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
